﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;

namespace MultiImageTransfer
{
    public partial class Form1 : Form
    {
        int destinationfolders;
        bool validated = false;
        IDictionary<int, string> folders = new Dictionary<int, string>();
        IDictionary<int, string> source_map = new Dictionary<int,string>();
        IDictionary<int, string> destination_map = new Dictionary<int, string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int.TryParse(destfolders.SelectedItem.ToString(), out destinationfolders);
                for (int x = 0; x < destinationfolders; x++)
                {
                    setfolders.Rows.Add();
                    setfolders.Rows[x].Cells[0].Value = x + 1;

                }
                setfolders.Update();
            }catch(Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }



        private void setfolders_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (destinationfolders > 0)
                {
                    selectfolder.ShowDialog();
                    setfolders.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = selectfolder.SelectedPath;
                    folders.Add(e.RowIndex + 1, selectfolder.SelectedPath);
                    destfolderslist.Items.Add(selectfolder.SelectedPath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }

        private void setfolders_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void validate_Click(object sender, EventArgs e)
        {
            try
            { 
            if(folders.Keys.Count!=destinationfolders)
            {
                MessageBox.Show("Validation failed!!!\n Select path for all the destination folders or reduce number of destination folders...");
                validated = false;
            }
            else if(folders.Keys.Count == destinationfolders)
            {
                MessageBox.Show("validation Succeeded!! Now you can move forward...");
                validated = true;
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }

        private void choosesource_Click(object sender, EventArgs e)
        {
            try
            { 
            if(validated)
            {
                destination.Visible = false;
                source.Visible = true;
                final.Visible = true;
                    destfolderslist.Items.Add("Delete");
            }
            else
            {
                validated = false;
                MessageBox.Show("Complete The Validation process first...");
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }



        private void getimages_Click(object sender, EventArgs e)
        {
            try { 
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Images (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF|" + "All files (*.*)|*.*";
            fd.Multiselect = true;
            fd.Title = "Select Images";
            DialogResult dr = fd.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                int imagenum = 1;
                foreach (String file in fd.FileNames)
                {
                    try{
                        source_map.Add(imagenum, file);
                        destination_map.Add(imagenum, "");
                        imagenum++;
                        Image m = Image.FromFile(file);
                        images.Images.Add(m);
                    }catch (Exception ex)
                    {MessageBox.Show("Error: " + ex.Message);}
                }

            }
            imagelistview.View = View.LargeIcon;
            imagelistview.LargeImageList = images;
            for (int x = 0; x < images.Images.Count; x++)
            {
                ListViewItem item = new ListViewItem();
                item.ImageIndex = x;
                imagelistview.Items.Add(item);
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }
        public bool ThumbnailCallback()
        {
            return false;
        }

        private void imagelistview_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            { 
            ListView.CheckedListViewItemCollection checkedimages = imagelistview.CheckedItems;
            foreach (ListViewItem x in checkedimages)
            {
                Image y=x.ImageList.Images[x.ImageIndex];
                string path = destfolderslist.SelectedItem.ToString() +'\\' +source_map[x.ImageIndex + 1].Split('\\').Last();
                destination_map[x.ImageIndex+1]=path;
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }

        private void final_Click(object sender, EventArgs e)
        {
            try { 
            for (int x = 0; x < source_map.Keys.Count; x++)
            {
                if (destination_map[x + 1] != "")
                {
                        if (destination_map[x + 1] == "Delete")
                        {
                            System.IO.File.Delete(source_map[x + 1]);
                        }
                        else
                        {
                            if (cutcopy.SelectedItem.ToString() == "Cut")
                            {
                                System.IO.File.Move(source_map[x + 1], destination_map[x + 1]);
                            }
                            else
                            {
                                System.IO.File.Copy(source_map[x + 1], destination_map[x + 1]);
                            }
                        }
                }
            }
            MessageBox.Show("Ooohhh Hooo... We finished copying files...Enjoyyy!!!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }

        private void clear_Click(object sender, EventArgs e)
        {
            try
            {
                source_map.Clear();
                destination_map.Clear();
                imagelistview.Items.Clear();
                images.Images.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }

        private void clear_list_Click(object sender, EventArgs e)
        {
            try
            {
                folders.Clear();
                destfolderslist.Items.Clear();
                setfolders.Rows.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }

        private void clearselection_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListViewItem x in imagelistview.CheckedItems)
                {
                    x.Checked = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Oopppsss....Something went wrong!!! hang on for error description");
                MessageBox.Show(ex.Message);
            }

        }

        private void back_Click(object sender, EventArgs e)
        {
            destination.Visible = true;
            source.Visible = false;
            final.Visible = false;

        }
    }
}
